package StructuralPattern.FlyWeight;

import java.util.HashMap;
import java.util.Map;

/**
 * @Classname : FlyWeightFactory
 * @Description : 享元工厂
 * @Author : chentianyu
 * @Date 2023/2/23 21:30
 */


public class FlyWeightFactory {
    // 享元池
    private static Map<String, FlyWeight> pool = new HashMap<>();

    // 获取享元对象
    public static FlyWeight getFlyWeight(String color) {
        if (pool.containsKey(color)) {
            return pool.get(color);
        } else {
            FlyWeight flyWeight = new ConcreteFlyWeight(color);
            pool.putIfAbsent(color, flyWeight);
            return flyWeight;
        }
    }
}
